package com.yonghui.springsecurityoauth2demo.service;

import cn.hutool.json.JSONUtil;
import com.yonghui.springsecurityoauth2demo.domain.OauthUser;
import com.yonghui.springsecurityoauth2demo.mapper.OauthUserMapper;
import com.yonghui.springsecurityoauth2demo.pojo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author liyonghui
 * @description: TODO
 * @date 2021/12/5 13:34
 */
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private OauthUserMapper oauthUserMapper;

    @Resource
    private AuthenticationManager authenticationManager;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        OauthUser oauthUser = oauthUserMapper.selectByUsername(username);

        if (oauthUser == null) {
            throw new UsernameNotFoundException("User not found with username: " + username);
        }

        // if (StringUtils.isNull(user))
        // {
        //     log.info("登录用户：{} 不存在.", username);
        //     throw new ServiceException("登录用户：" + username + " 不存在");
        // }
        // else if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
        // {
        //     log.info("登录用户：{} 已被删除.", username);
        //     throw new ServiceException("对不起，您的账号：" + username + " 已被删除");
        // }
        // else if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
        // {
        //     log.info("登录用户：{} 已被停用.", username);
        //     throw new ServiceException("对不起，您的账号：" + username + " 已停用");
        // }

        // 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
        // Authentication authentication = authenticationManager
        //         .authenticate(new UsernamePasswordAuthenticationToken(username, oauthUser.getPassword()));
        // LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        // System.out.println(JSONUtil.toJsonStr(loginUser));

        return createLoginUser(oauthUser);
    }
    // @Autowired
    // private PasswordEncoder passwordEncoder;
    //
    // @Override
    // public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    //     String password = passwordEncoder.encode("123456");
    //     return new User("admin", password, AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    // }

    public UserDetails createLoginUser(OauthUser user) {
        //return new LoginUser(user.getId(), user.getDeptId(), user, permissionService.getMenuPermission(user));
        Set<String> permissions = new HashSet<>();
        permissions.add("system");
        return new LoginUser(user, permissions);
    }
}
